﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Business.Service;
using AKMII.DMRA.Common;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.Service
{
    public class CommonService : ICommonService
    {
        private ReportManager reportManager;

        public CommonService()
        {
            reportManager = new ReportManager();
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public void SendReport(string account, int month, int year)
        {
            try
            {
                Logger.Info("Request from " + account);

                if (!string.IsNullOrEmpty(account))
                {
                    reportManager.SendReport(account, month, year);
                }
                else
                {
                    Logger.Info("Invalid MD name.");
                }

                Logger.Info("Request end " + account); 
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in SendReport service", ex);
            }
        }

        public void SendHistoryReport(string account, int month, int year, int type)
        {
            try
            {
                Logger.Info("Request from " + account);

                if (!string.IsNullOrEmpty(account))
                {
                    reportManager.SendReport(account, month, year, type);
                }
                else
                {
                    Logger.Info("Invalid MD name.");
                }

                Logger.Info("Request end " + account);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in Send History Report service", ex);
            }
        }

        public void SendMail(Mail mail)
        {
            try
            {
                if (mail != null)
                {
                    SmtpMailService mailService = new SmtpMailService(SystemConfiguration.CommonSetting.SmtpServer,
                        SystemConfiguration.CommonSetting.SystemMailAddress, SystemConfiguration.CommonSetting.MailPassword);
                    mailService.Send(mail);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in SendMail service", ex);
            }
        }
    }
}
